package cn.tedu.myvrd;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "MyFilter",urlPatterns = {"/vrd/send.html","/vrd/banner.html","/delete"})
public class MyFilter implements Filter {
    public void destroy() {
    }
//当请求的资源被拦截时执行
//    chain.doFilter(req, resp);执行代表放行,不执行代表拦截
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("拦截到了");
        HttpServletRequest request=(HttpServletRequest) req;
        HttpServletResponse response=(HttpServletResponse)resp;
//        获取session对象
        HttpSession session=request.getSession();
        if (session.getAttribute("user")==null){//没登录 重定向到登陆页面
            response.sendRedirect("/vrd/login.html");
        }else {//登录过 放行
            chain.doFilter(req, resp);
        }
    }
    public void init(FilterConfig config) throws ServletException {

    }

}
